package com.yhp.web;

import com.yhp.bo.AddressBO;
import com.yhp.pojo.UserAddress;
import com.yhp.service.AddressService;
import com.yhp.utils.JSONResult;
import com.yhp.utils.JsonUtils;
import com.yhp.utils.MobileEmailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "收货地址相关",tags = {"收货地址"})
@RequestMapping("/address")
public class AddressController {
    //用户在确认订单页面，会对收货地址做的操作
    //1.查询用户的所有收货地址
    //2.设置默认地址
    //3.收货地址的增删改

    @Autowired
    private AddressService addressService;

    //查询地址列表
    @PostMapping("/list")
    @ApiOperation(value = "查询地址列表",notes = "查询地址列表",httpMethod = "POST")
    public JSONResult getList(String userId){
        if(StringUtils.isBlank(userId)){
            return  JSONResult.errorMsg("用户id不能为空");
        }
        List<UserAddress> userAddresses = addressService.queryAll(userId);
        return  JSONResult.ok(userAddresses);
    }
    //新增地址
    @PostMapping("/add")
    @ApiOperation(value = "新增收货地址",notes = "新增收货地址",httpMethod = "POST")
    public JSONResult add(
            @RequestBody AddressBO addressBO){
        //验证数据有效性
        JSONResult jsonResult = checkAddress(addressBO);
        if(jsonResult.getStatus()!=200){
            return jsonResult;
        }
        addressService.addAddress(addressBO);
        return  JSONResult.ok();
    }

    //更新地址
    @PostMapping("/update")
    @ApiOperation(value = "更新收货地址",notes = "更新收货地址",httpMethod = "POST")
    public JSONResult update(
            @RequestBody AddressBO addressBO){
        //验证数据有效性
        if(addressBO.getAddressId()==null){
            return JSONResult.errorMsg("地址不能为null");
        }

        JSONResult jsonResult = checkAddress(addressBO);
        if(jsonResult.getStatus()!=200){
            return jsonResult;
        }
        addressService.updateAddress(addressBO);
        return  JSONResult.ok();
    }

    //删除地址
    @PostMapping("/delete")
    @ApiOperation(value = "删除收货地址",notes = "删除收货地址",httpMethod = "POST")
    public JSONResult delete(
            @ApiParam(name="userId",value = "被删除地址的用户id",required = true)String userId,
            @ApiParam(name="addressId",value = "被删除地址id",required = true)String addressId){
        //验证数据有效性
        if(StringUtils.isBlank(userId)||StringUtils.isBlank(addressId)){
            return JSONResult.errorMsg("数据不全");
        }
        addressService.deleteAddress(userId, addressId);
        return  JSONResult.ok();
    }
    //设置默认地址
    @PostMapping("/setDefalut")
    @ApiOperation(value = "设置默认收货地址",notes = "设置默认收货地址",httpMethod = "POST")
    public JSONResult setDefault(
            @ApiParam(name="userId",value = "设置默认地址的用户id",required = true)String userId,
            @ApiParam(name="addressId",value = "设置默认地址的地址id",required = true)String addressId){
        //验证数据有效性
        if(StringUtils.isBlank(userId)||StringUtils.isBlank(addressId)){
            return JSONResult.errorMsg("数据不全");
        }
        addressService.setDefaultAddress(userId, addressId);
        return  JSONResult.ok();
    }


    private JSONResult checkAddress(AddressBO addressBO){
        if(StringUtils.isBlank(addressBO.getUserId())||
                StringUtils.isBlank(addressBO.getReceiver())||
                StringUtils.isBlank(addressBO.getMobile())||
                StringUtils.isBlank(addressBO.getProvince())||
                StringUtils.isBlank(addressBO.getCity())){
            return  JSONResult.errorMsg("收货信息不能为空");
        }
        if(!MobileEmailUtils.checkMobileIsOk(addressBO.getMobile())){
            return  JSONResult.errorMsg("电话格式不正确");
        }
        return JSONResult.ok();
    }

}
